package gui.view;

import gui.parser.Attr;

public class LinearLayout extends ViewGroup {
	private int orientation;
	public static int HORIZONTAL = 0x0;
	public static int VERTICAL = 0x1;

	public LinearLayout(Attr attr) {
		super(attr);
	}

	@Override
	public void init() {
		super.init();
		String m;
		m = getAttr("gui:orientation");
		if (m != null) {
			orientation = m.toLowerCase().equals("vertical") ? VERTICAL : HORIZONTAL;
		}
	}

	@Override
	public void invalidate() {
		super.invalidate();
		int x_count = 0, y_count = 0;
		for (View child : getChildren()) {
			if (orientation == HORIZONTAL) {
				child.onMeasure(x_count += child.margin_left, child.margin_top, child.getHeight(), child.getWidth());
				x_count = x_count + child.getWidth() + child.margin_right;
			} else {
				child.onMeasure(child.margin_left, y_count += child.margin_top, child.getHeight(), child.getWidth());
				y_count = y_count + child.getHeight() + child.margin_bottom;
			}
		}
	}
}
